package com.kcht.itsed.mws.web;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.jms.Queue;
import javax.jms.Topic;

@SpringBootApplication
@EnableAsync
@EnableJms
@ComponentScan(basePackages = "com.kcht.itsed.mws")
@MapperScan(basePackages = "com.kcht.itsed.mws.persist.dao")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }



    /**
     * 队列存储的内容是RFID原始标签信息
     * */
    @Bean
    public Queue originalTagResult(){
        return new ActiveMQQueue("RFID原始标签");
    }

    /**
     * 队列模式的JmsTemplate
     * */
    @Bean("queueJmsTemplate")
    public JmsTemplate queueJmsTemplate(SingleConnectionFactory connectionFactory){
        JmsTemplate queueJmsTemplate=new JmsTemplate(connectionFactory);
        queueJmsTemplate.setPubSubDomain(false);
        queueJmsTemplate.setExplicitQosEnabled(true);
        queueJmsTemplate.setDeliveryPersistent(false);
        queueJmsTemplate.setTimeToLive(120*1000); //设置数据120s后失效
        return queueJmsTemplate;
    }
    /**
     * jms监听队列的容器
     * */
    @Bean("queueFactory")
    public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(SingleConnectionFactory connectionFactory){
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(false);
        return factory;
    }

    /**
     * 主题是订阅者的报告
     * */
    @Bean
    public Topic  subscribedReportTopic(){
        return new ActiveMQTopic("ALE订阅者的报告");
    }

    /**
     * 发布/订阅模式的JmsTemplate
     * */
    @Bean("topicJmsTemplate")
    public JmsTemplate topicJmsTemplate(SingleConnectionFactory connectionFactory){
        JmsTemplate topicJmsTemplate=new JmsTemplate(connectionFactory);
        topicJmsTemplate.setPubSubDomain(true);
        topicJmsTemplate.setExplicitQosEnabled(true);
        topicJmsTemplate.setDeliveryPersistent(false);
        return topicJmsTemplate;
    }
    /**
     * jms监听主题的容器
     * */
    @Bean("topicFactory")
    public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(SingleConnectionFactory connectionFactory){
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }

    /**
     * 线程池，
     * 核心数量=阅读器数量*2
     * 线程最大数量设置为最大
     * 任务阻塞队列设置为0,不想让其阻塞
     * */
    @Bean
    public ThreadPoolTaskExecutor aleThreadPool(){
        ThreadPoolTaskExecutor aleThreadPool=new ThreadPoolTaskExecutor();
        aleThreadPool.setCorePoolSize(40);
        aleThreadPool.setQueueCapacity(0);
        return aleThreadPool;
    }
}
